<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px;">
                    <div class="user-info">
                        <img src="../../assets/img/img.jpg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>管理员</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        时间：
                        <span>2020-09-01</span>
                    </div>
                    <div class="user-info-list">
                        小组：
                        <span>素质教育漏网之鱼小队</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:252px;">
                    <div slot="header" class="clearfix">
                        <span>消息提示</span>
                    </div>

                    <el-table :show-header="false" :data="todoList" style="width:100%;">
                        <el-table-column width="40">
                            <template slot-scope="scope">
                                <el-checkbox v-model="scope.row.status"></el-checkbox>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <div
                                    class="todo-item"
                                    :class="{'todo-item-del': scope.row.status}"
                                >{{scope.row.title}}</div>
                            </template>
                        </el-table-column>
                        <el-table-column width="60"></el-table-column>
                    </el-table>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ userCount }}</div>
                                    <div>用户总量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{newRepotCount}}</div>
                                    <div>举报推送</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-circle-plus-outline grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{newUserCount}}</div>
                                    <div>新增用户</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height:800px;">
                    <div slot="header" class="clearfix">
                        <span>项目结构</span>
                    </div>
                    <div >
<!--                    <img src="../../assets/img/me.png" alt width="600px" height="300px" style="margin-left:15%"/>-->
                        <el-col :span="14">

                            <div class="graph" >
                                <el-card shadow="hover" style="background-color:#F5FFFA;width: 830px" >
                                    <!--                    <echart :chartData="frontData" style="height: 260px" :isAxisChart="false"></echart>-->
                                    <div id="frontchartPie" style=" width:100%;height:300px;"></div>
                                    <p style="text-align: center;color:#778899">前端项目结构</p>
                                </el-card>
                                <el-card shadow="hover" style="background-color:#F5FFFA;width: 830px">
                                    <!--                    <echart :chartData="frontData" style="height: 260px" :isAxisChart="false"></echart>-->
                                    <div id="backchartPie" style=" width:100%;height:300px;"></div>
                                    <p style="text-align: center;color:#778899">后端项目结构</p>
                                </el-card>
                            </div>
                            <div class="num" style="margin-top: 60px">
                                <el-card shadow="hover" v-for="item in countData" :key="item.value"
                                         :body-style="{ display: 'flex', padding: 0 }"
                                         style="background-color:#FFFAFA">
                                    <img class="icon" :src="item.icon"/>
                                    <div class="detail">
                                        <p class="txt">{{ item.value }}</p>
                                    </div>
                                </el-card>
                            </div>
                        </el-col>

                    </div>

                </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
                <schart class="schart" ref="bar" canvasId="bar" :options="options1"></schart>
            </el-col>
            <el-col :span="12">
                <schart class="schart" ref="line" canvasId="line" :options="options2"></schart>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
                <schart class="schart" ref="pie" canvasId="pie" :options="options3"></schart>
            </el-col>
            <el-col :span="12">
                <schart class="schart" ref="ring" canvasId="ring" :options="options4"></schart>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import {resultcount} from '../../network/result'
import { countDeptEdu } from '../../api/apis/deptment';
import { countEmp } from '../../api/apis/emp';
import { countEmpSex } from '../../api/apis/emp';
import { postlist } from '../../network/post';

import * as echarts from 'echarts'

require('echarts/theme/macarons')
export default {
    name: 'dashboard',
    data() {
        return {
            userCount:0 ,
            newUserCount:0,
            newRepotCount:0,
            // options3: {
            //     type: 'pie',
            //     title: {
            //         text: '公司人员学历总结'
            //     },
            //     legend: {
            //         position: 'left'
            //     },
            //     bgColor: '#fbfbfb',
            //     labels: ['研究生', '本科生', '大专生', '高中生以以下'],
            //     datasets: [
            //         {
            //             data: []
            //         }
            //     ]
            // },
            // options4: {
            //     type: 'ring',
            //     title: {
            //         text: '公司性别比例'
            //     },
            //     showValue: false,
            //     legend: {
            //         position: 'bottom',
            //         bottom: 40
            //     },
            //     bgColor: '#fbfbfb',
            //     labels: ['男', '女'],
            //     datasets: [
            //         {
            //             data: [500, 500]
            //         }
            //     ]
            // },
            name: localStorage.getItem('ms_username'),
            todoList: [
                {
                    title: '今天新增了10个用户',
                    status: false
                },
                {
                    title: '流浪码农对体育版块进行了更新',
                    status: false
                },
                {
                    title: 'sjm上传了一篇文章',
                    status: false
                },
                {
                    title: '流浪码农给你留言了',
                    status: false
                }
            ],
            data: [
                {
                    name: '2018/09/04',
                    value: 1083
                },
                {
                    name: '2018/09/05',
                    value: 941
                },
                {
                    name: '2018/09/06',
                    value: 1139
                },
                {
                    name: '2018/09/07',
                    value: 816
                },
                {
                    name: '2018/09/08',
                    value: 327
                },
                {
                    name: '2018/09/09',
                    value: 228
                },
                {
                    name: '2018/09/10',
                    value: 1065
                }
            ],

        };
    },

    components: {
        Schart
    },

    created() {
        this.getData();
        this.getOptions1Data();
        this.getOptions2Data();
        this.getOptions4Data();

    },
    mounted () {
        this.$nextTick(() => {
            this.drawPieChart()
        })
    },
    methods: {
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        },
        getData(){
            resultcount()
                .then((res) => {
                    console.log(res);
                    if (res.code === 200) {
                        console.log(res);
                        this.userCount = res.data.userCount;
                        this.newUserCount=res.data.newUserCount;
                        this.newRepotCount=res.data.newRepotCount;

                        // this.total
                        this.current=res.data.current;
                        this.size=res.data.size;
                        this.total = res.data.total;
                    } else {
                        this.$message.error('数据获取失败，' + res.msg);
                    }
                })
                .catch(() => {
                    this.$message.error('数据获取失败，请在保证网络畅通的情况下检查请求的地址或参数是否正确');
                });
        },
        //各部门人员学历统计
        getOptions1Data() {
            countDeptEdu().then((res) => {
                let list = res.list;
                // callback(res.list);
                let postgraduate = 0;
                let undergraduate = 0;
                let juniorCollege = 0;
                let highSchool = 0;

                for (let i = 0; i < list.length; i++) {
                    //options1
                    this.options1.labels[i] = list[i].deptName;
                    this.options1.datasets[0].data[i] = list[i].postgraduate;
                    this.options1.datasets[1].data[i] = list[i].undergraduate;
                    this.options1.datasets[2].data[i] = list[i].juniorCollege;
                    this.options1.datasets[3].data[i] = list[i].highSchool;

                    //options3
                    postgraduate = postgraduate + list[i].postgraduate; //公司的研究生数量
                    undergraduate = undergraduate + list[i].undergraduate; //公司的本科生数量
                    juniorCollege = juniorCollege + list[i].juniorCollege; //公司的大专生数量
                    highSchool = highSchool + list[i].highSchool; //公司的高中生及以下数量
                }

                console.log('options1');
                console.log(this.options1);

                //重新渲染数据，这很重要
                //柱状图重新渲染数据
                this.$refs.bar.renderChart();

                //options3
                this.options3.datasets[0].data[0] = postgraduate;
                this.options3.datasets[0].data[1] = undergraduate;
                this.options3.datasets[0].data[2] = juniorCollege;
                this.options3.datasets[0].data[3] = highSchool;

                console.log('options3');
                console.log(this.options3);

                //饼状图重新渲染数据
                this.$refs.pie.renderChart();
            });
        },

        //最近几个月公司员工变动统计
        getOptions2Data() {
            countEmp().then((res) => {
                let list = res.list;
                for (let i = 0; i < list.length; i++) {
                    this.options2.labels[i] = list[i].month;

                    this.options2.datasets[0].data[i] = list[i].newEmp;
                    this.options2.datasets[1].data[i] = list[i].dimisEmp;
                }

                console.log('options2');
                console.log(this.options2);

                //重新渲染折线图
                this.$refs.line.renderChart();
            });
        },

        //公司性别比例统计
        getOptions4Data() {
            countEmpSex().then((res) => {
                let list = res.list;
                this.options4.datasets[0].data[0] = list[0].male;
                this.options4.datasets[0].data[1] = list[0].female;

                console.log('options4');
                console.log(this.options4);

                //重新渲染环形图
                this.$refs.ring.renderChart();
            });
        },
        //图
        drawPieChart () {
            const mytextStyle = {
                color: '#333',
                fontSize: 12
            }
            const mylabel = {
                show: true,
                position: 'right',
                offset: [30, 40],
                formatter: '{b} : {c} ({d}%)',
                textStyle: mytextStyle
            }
            this.frontchartPie = echarts.init(document.getElementById('frontchartPie'), 'macarons')
            this.frontchartPie.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}:{d}%'
                },
                series: [
                    {
                        type: 'pie',
                        // roseType: 'angle',
                        data: [
                            {
                                name: 'Vue',
                                value: 80.1
                            },
                            {
                                name: 'JavaScript',
                                value: 15.6
                            },
                            {
                                name: 'CSS',
                                value: 3.4
                            },
                            {
                                name: 'HTML',
                                value: 0.9
                            }
                        ],
                        label: {
                            emphasis: mylabel
                        }
                    }
                ]
            })
            this.backchartPie = echarts.init(document.getElementById('backchartPie'), 'macarons')
            this.backchartPie.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}:{d}%'
                },
                series: [
                    {
                        type: 'pie',
                        roseType: 'angle',
                        data: [
                            {
                                name: 'Java',
                                value: 100
                            }
                            // {
                            //   name: 'JavaScript',
                            //   value: 14.2
                            // },
                            // {
                            //   name: 'CSS',
                            //   value: 0.9
                            // }
                        ],
                        label: {
                            emphasis: mylabel
                        }
                    }
                ]
            })
        }

    }
};
</script>


<style scoped>
.el-row {
    
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
    .graph {
        display: flex;
        flex-direction:column;
    }
</style>
